package com.kelvin.backend.common.util.date;

import cn.hutool.core.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * <p>Title: DateUtil</p>
 * <p>Description: </p>
 *
 * @author Kelvin.Chen
 * @blame Lenovo
 * @datetime 2020/7/1 17:32
 */
public class DateUtil {

    private static final Logger log = LoggerFactory.getLogger(DateUtil.class);

    /**
     * 获取日期转换器
     *
     * @param format
     * @return
     */
    public static SimpleDateFormat getDateFormat(String format) {
        format = ObjectUtil.isEmpty(format) ? DatePattern.NORMAL_DATETIME_PATTERN : format;
        return new SimpleDateFormat(format);
    }

    /**
     * 根据日期字符串获取日期
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static Date getDate(String dateStr, String format) {
        Date date = null;
        if (ObjectUtil.isNotEmpty(dateStr)) {
            if (ObjectUtil.isEmpty(format)) {
                format = DatePattern.NORMAL_DATETIME_PATTERN;
            }
            try {
                date = getDateFormat(format).parse(dateStr);
            } catch (ParseException e) {
                log.error("[DateUtils]出现异常", e);
            }
        }

        return date;
    }

    /**
     * 获取当前日期
     *
     * @param date
     * @param format
     * @return
     */
    public static Date getDate(Date date, String format) {
        String dateStr = getDateStr(date, format);
        return getDate(dateStr, format);
    }

    /**
     * 根据时间获取时间字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String getDateStr(Date date, String format) {
        return getDateFormat(format).format(date);
    }

    /**
     * 获取毫秒时间
     *
     * @param dateStr
     * @return
     */
    public static Long getDateTime(String dateStr) {
        Date date = getDate(dateStr, null);
        return null == date ? null : date.getTime();
    }

    /**
     * 时间戳转日期字符串
     *
     * @param timeMills
     * @param format
     * @return
     */
    public static String getDateStrFromTimeMills(long timeMills, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeMills);

        return getDateStr(calendar.getTime(), format);
    }

    /**
     * 时间戳转日期
     *
     * @param timeMills
     * @param format
     * @return
     */
    public static Date getDateFromTimeMills(long timeMills, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeMills);

        return calendar.getTime();
    }

    /**
     * UTC时间转为本地时间
     *
     * @param utcTime
     * @param format
     * @return
     */
    public static Date utc2Local(String utcTime, String format) {
        SimpleDateFormat dateFormat = getDateFormat(format);
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date utcDate;
        Date localDate = null;
        try {
            utcDate = dateFormat.parse(utcTime);
            dateFormat.setTimeZone(TimeZone.getDefault());
            String localTime = dateFormat.format(utcDate.getTime());

            localDate = dateFormat.parse(localTime);
        } catch (ParseException e) {
            log.error("[utc2Local]出现异常", e);
        }

        return localDate;
    }

    /**
     * 通过天计算日期
     *
     * @param originalDate
     * @param timeUnit     计算时间单位
     * @param num          计算的时间数 Calendar类中的常量
     * @param plusOrMinus  加或减 plus + ; minus -
     * @return
     */
    public static Date calculateDate(Date originalDate, int timeUnit, int num, String plusOrMinus) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(originalDate);

        if (DatePattern.PLUS.equals(plusOrMinus)) {
            calendar.add(timeUnit, num);
        } else if (DatePattern.MINUS.equals(plusOrMinus)) {
            calendar.add(timeUnit, -num);
        }

        return calendar.getTime();
    }
}
